<!--项目公司考核模板-->
<template>
  <div class="newCheckContainer">
    <!--行政村1图片预览开始-->

    <div class="preViewDiv" v-if="preViewshow">
      <div class="preViewDivTop">
        <div class="custom-indicator" slot="indicator">{{ current + 1 }}/{{photoimg.length}}</div>
        <van-icon name="clear" class="closeable" @click="preViewshow=!preViewshow" />
      </div>
      <van-swipe class="my-swipe" @change="onChange" :show-indicators="false">
        <van-swipe-item v-bind:key="index" v-for="(item,index) in photoimg">
          <van-icon name="delete" class="deleteable" @click="del(current)" />
          <van-image :src="item.url" class="bannerimg" fit="cover" height="100%" width="100%" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!--图片预览结束-->
    <!--行政村2图片预览开始-->

    <div class="preViewDiv" v-if="twopreViewshow">
      <div class="preViewDivTop">
        <div class="custom-indicator" slot="indicator">{{ twocurrent + 1 }}/{{twophotoimg.length}}</div>
        <van-icon name="clear" class="closeable" @click="twopreViewshow=!twopreViewshow" />
      </div>
      <van-swipe class="my-swipe" @change="twoonChange" :show-indicators="false">
        <van-swipe-item v-bind:key="index" v-for="(item,index) in twophotoimg">
          <van-icon name="delete" class="deleteable" @click="twodel(twocurrent)" />
          <van-image :src="item.url" class="bannerimg" fit="cover" height="100%" width="100%" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!--图片预览结束-->
    <!--头部bar开始-->
    <CommonBar :options="options"></CommonBar>
    <!--头部bar结束-->
    <div class="divheight"></div>
    <!--考核员指标开始-->
    <div class="zhibiao">
      <p :class="ellipsis?'ellipsis':'unellipsis'" class="zhibiaoitem descontent">
        <span v-for="item in TemplateKpiTop" :key="item.id">
          {{item.remark}}
          <br />
        </span>
      </p>
      <p class="showdesc" @click="ela">
        {{ellipsis?'展开':'收起'}}
        <van-icon :name="ellipsis?'arrow-down':'arrow-up'" class="arrowicon" />
      </p>
    </div>
    <!--考核员指标结束-->
    <div class="divheight"></div>
    <!--固定项目部项目公司开始-->
    <div class="areacontent">
      <!---项目公司-->
      <van-row gutter="30">
        <van-col span="12">
          <van-dropdown-menu active-color="#0AD691">
            <van-dropdown-item
              v-model="ProjectCompany"
              :options="ProjectCompanycolumns"
              @change="onConfirmProjectCompany"
            />
          </van-dropdown-menu>
        </van-col>
        <!---项目部-->
        <van-col span="12">
          <van-dropdown-menu active-color="#0AD691">
            <van-dropdown-item
              v-model="Project"
              :options="Projectcolumns"
              @change="onConfirmProject"
            />
          </van-dropdown-menu>
        </van-col>
      </van-row>
    </div>
    <!--固定项目部项目公司结束-->
    <div class="divheight"></div>
    <!--选择第一个行政村开始-->
    <div class="choseContent">
      <p class="title">考核第一个行政村</p>
      <!--选行政区开始-->
      <div class="areacontent">
        <!---stamsid!=0 项目公司-项目部-行政村
      stamsid=0 项目部-行政村
        -->
        <!---项目公司-->
        <van-row gutter="30">
          <van-col span="12">
            <p class="chosexz" @click="xzshow=true">{{xzname}}</p>
            <!---行政村弹框-->
            <van-popup v-model="xzshow" position="bottom" :style="{ height: '40%' }" class="popdiv">
              <div class="popitle">
                <div class="searchxp">
                  <input type="text" v-model="searchx" class="searchx" placeholder="搜索行政村" />
                </div>
              </div>
              <ul class="pop_ul">
                <li
                  class="pop_li"
                  v-for="(item,index) in  NewItems"
                  :key="index"
                  :class="{ current: tabCurrentIndex === index }"
                  @click="chosexz(item.value,index)"
                >{{item.text}}</li>
                <li v-show="nodat" class="pop_li">暂无匹配数据</li>
              </ul>
            </van-popup>
          </van-col>
        </van-row>
      </div>
      <!--选行政区结束-->
      <div class="divheight"></div>
      <!--打分开始-->
      <div class="scorecontent">
        <div class="countScoreDiv">
          <div class="countScoreTop">
            <p>
              项目部：
              <span>{{clearnName}}</span>
            </p>
            <p>{{signdate}}</p>
          </div>
        </div>
        <div class="countScoreHeader">
          <van-row>
            <van-col :span="12">考核项名称</van-col>
            <van-col :span="6">得分</van-col>
            <van-col :span="3" class="textcenter">拍照</van-col>
            <van-col :span="3" class="textright">查看</van-col>
          </van-row>
        </div>
        <div>
          <van-row v-for="(item ,$index) in TemplateKpi" :key="$index" class="rowItem">
            <van-col :span="12">{{item.name}}({{item.newscore}}分)</van-col>
            <van-col :span="6">
              <input
                type="number"
                v-model="item.score"
                class="scoreinput"
                @blur="computsocre(item.score,item.newscore)"
              />
            </van-col>
            <van-col :span="3" @click="takephoto($index)" class="textcenter">
              <van-image width="22" height="22" :src="takephoto1" style="vertical-align: middle;" />
            </van-col>
            <van-col :span="3" class="textright" @click="ViewImg($index)">
              <van-image
                width="22"
                height="22"
                :src="item.viewimg"
                style="vertical-align: middle;"
              />
            </van-col>
          </van-row>
          <van-action-sheet v-model="takephotoshow" :actions="actions" @select="takephotoSelect" />
        </div>
        <p class="allcount">
          总分:
          <span>{{sumconunt}}</span>
        </p>
      </div>
    </div>
    <!--选择第一个行政村结束-->
    <div class="divheight"></div>
    <!--选择第二个行政村开始-->
    <div class="choseContent">
      <p class="title">考核第二个行政村</p>
      <!--选行政区开始-->
      <div class="areacontent">
        <!---stamsid!=0 项目公司-项目部-行政村
      stamsid=0 项目部-行政村
        -->
        <!---项目公司-->
        <van-row gutter="30">
          <van-col span="12">
            <p class="chosexz" @click="twoxzshow=true">{{twoxzname}}</p>
            <!---行政村弹框-->
            <van-popup
              v-model="twoxzshow"
              position="bottom"
              :style="{ height: '40%' }"
              class="popdiv"
            >
              <div class="popitle">
                <div class="searchxp">
                  <input type="text" v-model="searchx" class="searchx" placeholder="搜索行政村" />
                </div>
              </div>
              <ul class="pop_ul">
                <li
                  class="pop_li"
                  v-for="(item,index) in  NewItems"
                  :key="index"
                  :class="{ current: twotabCurrentIndex === index }"
                  @click="twochosexz(item.value,index)"
                >{{item.text}}</li>
                <li v-show="nodat" class="pop_li">暂无匹配数据</li>
              </ul>
            </van-popup>
          </van-col>
        </van-row>
      </div>
      <!--选行政区结束-->
      <div class="divheight"></div>
      <!--打分开始-->
      <div class="scorecontent">
        <div class="countScoreDiv">
          <div class="countScoreTop">
            <p>
              项目部：
              <span>{{clearnName}}</span>
            </p>
            <p>{{signdate}}</p>
          </div>
        </div>
        <div class="countScoreHeader">
          <van-row>
            <van-col :span="12">考核项名称</van-col>
            <van-col :span="6">得分</van-col>
            <van-col :span="3" class="textcenter">拍照</van-col>
            <van-col :span="3" class="textright">查看</van-col>
          </van-row>
        </div>
        <div>
          <van-row v-for="(item ,$index) in twoTemplateKpi" :key="$index" class="rowItem">
            <van-col :span="12">{{item.twoname}}({{item.twonewscore}}分)</van-col>
            <van-col :span="6">
              <input
                type="number"
                v-model="item.twoscore"
                class="scoreinput"
                @blur="computsocre(item.twoscore,item.twonewscore)"
              />
            </van-col>
            <van-col :span="3" @click="twotakephoto($index)" class="textcenter">
              <van-image width="22" height="22" :src="takephoto1" style="vertical-align: middle;" />
            </van-col>
            <van-col :span="3" class="textright" @click="twoViewImg($index)">
              <van-image
                width="22"
                height="22"
                :src="item.viewimg"
                style="vertical-align: middle;"
              />
            </van-col>
          </van-row>
          <van-action-sheet
            v-model="twotakephotoshow"
            :actions="actions"
            @select="twotakephotoSelect"
          />
        </div>
        <p class="allcount">
          总分:
          <span>{{twosumconunt}}</span>
        </p>
      </div>
    </div>
    <!--选择第额个行政村结束-->

    <!--按钮-->
    <div class="buttonbot">
      <p class="pass" @click="savedata">保存</p>
      <p class="refuse" @click="submit">提交</p>
    </div>
    <!--加载框开始-->
    <van-overlay :show="showloading">
      <div class="wrapper">
        <div class="loadingcont">
          <van-loading size="24px" vertical>{{loaddingText}}</van-loading>
        </div>
      </div>
    </van-overlay>
    <!--加载框结束-->
    <!--打分结束-->
  </div>
</template>
<script>
import { Dialog } from "vant";
import { ImagePreview } from "vant";
import { compressImage } from "../../common/compressImage";
import CommonBar from "../../common/commonBar";
import { mapState } from "vuex";
export default {
  components: {
    CommonBar
  },
  computed: {
    ...mapState({
      token: state => state.Authorization,
      user_department_type: state => state.userData.department_type, //人员类型
      user_department_id: state => state.userData.department_id, // 人员id
      user_group_department_id: state => state.userData.group_department_id,
      user_project_company_id: state => state.userData.project_company_id,
      user_project_id: state => state.userData.project_id,
      user_role_id: state => state.userData.role_id,
      sanitation_id: state => state.userData.sanitation_management_department_id
    }),
    NewItems() {
      var _this = this;
      var NewItems = [];
      this.xzList.map(function(item) {
        if (item.text.search(_this.searchx) != -1) {
          _this.nodat = false;
          NewItems.push(item);
        } else {
          _this.nodat = true;
        }
      });
      return NewItems;
    }
  },

  data() {
    return {
      ellipsis: true,
      takephoto1: require("../../../../static/image/takephoto.png"),
      viewImg1: require("../../../../static/image/viewImg.png"),
      viewImghui: require("../../../../static/image/viewimghui.png"),
      //加载框
      showloading: false,
      loaddingText: "加载中",

      //公共bar
      options: {
        type: "icon",
        centerText: "项目公司指标考核",
        iconNname: "wap-home",
        lefttype: "left"
      },
      twoTemplateKpi: [],
      TemplateKpi: [], //指标表格信息
      TemplateKpiTop: [], //指标内容
      //项目公司
      ProjectCompany: 0,
      ProjectCompanycolumns: [{ text: "请选择项目公司", value: 0 }],
      //项目部
      Project: 0,
      Projectcolumns: [{ text: "请选择项目部", value: 0 }],
      //片区
      District: 0,
      Districtcolumns: [{ text: "请选择行政村", value: 0 }],
      //片区

      clearnName: "", //项目部名称
      district_id: "", //项目部所在片区
      signdate: "", //当前日期
      scoreinput: "",
      photo: [],
      detailresult: [],
      //相机
      actions: [
        { name: "相机", id: "1" },
        { name: "图库", id: "2" },
        { name: "取消", id: "3" }
      ],
      takephotoshow: false,

      currentLng: "",
      currentLat: "",
      currentDateTime: "",
      imgUrl: "",
      countindex: "",

      //查看图片
      showimg: false,
      imgext: "",
      //参数赋值
      group_department_id: "",
      project_company_id: "",
      project_id: "",
      district_id1: "",
      duty_id: "",
      template_id: "",
      is_power: "", //判断是否能提交
      //行政村
      project_address: "", //行政村地址
      project_addrcode: "", //编码
      project_List: [],
      //预览图片带删除
      preViewshow: false,
      current: 0,
      photoimg: [],
      TemplateKpi1: [],
      twoTemplateKpi1: [],
      sumconunt: 0,
      twosumconunt: 0,
      arrchenge: "",
      //行政村搜索
      xzshow: false,
      searchx: "", //搜索行政村
      xzname: "请选择行政村1",
      xzList: [],
      tabCurrentIndex: 0,
      nodat: false,
      //第二个行政cun
      twotabCurrentIndex: 0,
      twoxzname: "请选择行政村2",
      two_project_address: "", //行政村地址
      two_project_addrcode: "", //编码
      twoxzshow: false,
      //拍照和相册
      twotakephotoshow: false,
      twocountindex: "",
      twophoto: [],
      twophotoimg: [],
      twopreViewshow: false,
      twocurrent: 0
    };
  },
  created() {
    // var params = {
    //   project_company_id: this.user_project_company_id,
    //   token: this.token
    // };
    // this.initThird(params);
    this.initFirst();
    this.getloadData();
    this.showDate();
  },
  //当拍照改变后photo数组就为空
  watch: {
    countindex(newval, oldval) {
      console.log(newval, oldval);
      this.photo = [];
    },
    twocountindex(newval, oldval) {
      console.log(newval, oldval);
      this.twophoto = [];
    },
    TemplateKpi: {
      handler: function(newVal, oldval) {
        console.log("监听", newVal);
        if (oldval.length <= 0) {
          this.arrchenge = false;
        } else {
          this.arrchenge = true;
        }
        let new_arr = newVal.map(obj => {
          return obj.score;
        });
        var r = new_arr.filter(function(s) {
          return s && s.trim();
        });
        var sum = 0;
        for (let i = 0; i < r.length; i++) {
          sum += parseInt(r[i]);
        }
        this.sumconunt = sum;
      },
      deep: true //深度监听
    },
    twoTemplateKpi: {
      handler: function(newVal, oldval) {
        console.log("监听", newVal);
        if (oldval.length <= 0) {
          this.arrchenge = false;
        } else {
          this.arrchenge = true;
        }
        let new_arr = newVal.map(obj => {
          return obj.twoscore;
        });
        var r = new_arr.filter(function(s) {
          return s && s.trim();
        });
        var sum = 0;
        for (let i = 0; i < r.length; i++) {
          sum += parseInt(r[i]);
        }
        this.twosumconunt = sum;
      },
      deep: true //深度监听
    }
  },
  destroyed() {
    var that = this;
    document.removeEventListener("backbutton", that.backbutton, false);
  },
  mounted() {
    var that = this;
    document.addEventListener("backbutton", that.backbutton, false);
  },
  methods: {
    backbutton() {
      if (this.arrchenge) {
        Dialog.confirm({
          title: "提示",
          message: "内容未保存，是否返回"
        })
          .then(() => {
            history.back();
          })
          .catch(() => {
            // on cancel
          });
      } else {
        history.back();
      }
    },
    computsocre(score, newscore) {
      console.log(score, newscore);
      if (score > newscore) {
        this.$toast({ message: "考核分数不能大于指标分数" });
      }
    },
    ela() {
      this.ellipsis = !this.ellipsis;
    },
    /**
     * onChange预览照片时的轮播方法
     * del删除预览照片
     */
    onChange(index) {
      this.current = index;
    },
    twoonChange(index) {
      this.twocurrent = index;
    },
    del(id) {
      Dialog.confirm({
        title: "提示",
        message: "是否删除该照片"
      })
        .then(() => {
          this.photoimg.splice(id, 1);
          this.current = 0;
          if (this.photoimg.length == 0) {
            this.current = -1;
          }
        })
        .catch(() => {
          // on cancel
        });
    },
    twodel(id) {
      Dialog.confirm({
        title: "提示",
        message: "是否删除该照片"
      })
        .then(() => {
          this.twophotoimg.splice(id, 1);
          this.twocurrent = 0;
          if (this.twophotoimg.length == 0) {
            this.twocurrent = -1;
          }
        })
        .catch(() => {
          // on cancel
        });
    },
    //拍照事件
    takephoto(template_id) {
      this.takephotoshow = true;
      this.countindex = template_id;
    },
    //拍照事件2
    twotakephoto(template_id) {
      this.twotakephotoshow = true;
      this.twocountindex = template_id;
    },
    //选中图片方式，1是相机，2是图库
    takephotoSelect(item) {
      this.takephotoshow = false;
      if (item.id == 3) {
        this.$toast({ message: "取消上传图片" });
      } else if (item.id == 2) {
        var srcType = Camera.PictureSourceType.PHOTOLIBRARY;
        this.$nextTick(() => {
          this.getLoation();
        });
        this.showTime(); //获取当前时间
        this.cameraTakePicture(srcType); //调用相机
       
      } else if (item.id == 1) {
        var srcType = Camera.PictureSourceType.CAMERA;
        this.$nextTick(() => {
          this.getLoation();
        });
        this.showTime(); //获取当前时间
        this.cameraTakePicture(srcType); //调用相机
      } else if (
        this.TemplateKpi[this.countindex].photo &&
        this.TemplateKpi[this.countindex].photo.length >= 5
      ) {
        this.$toast({ message: "不能上传超过5张" });
      }
    },
    //选中图片方式，1是相机，2是图库
    twotakephotoSelect(item) {
      this.twotakephotoshow = false;
      if (item.id == 3) {
        this.$toast({ message: "取消上传图片" });
      } else if (item.id == 2) {
        
        var srcType = Camera.PictureSourceType.PHOTOLIBRARY;
        this.$nextTick(() => {
          this.getLoation();
        });
        this.showTime(); //获取当前时间
        this.twocameraTakePicture(srcType); //调用相机
      } else if (item.id == 1) {
       
        var srcType = Camera.PictureSourceType.CAMERA;
        this.$nextTick(() => {
          this.getLoation();
        });
        this.showTime(); //获取当前时间
        this.twocameraTakePicture(srcType); //调用相机
      } else if (
        this.twoTemplateKpi[this.twocountindex].photo &&
        this.twoTemplateKpi[this.twocountindex].photo.length >= 5
      ) {
        this.$toast({ message: "不能上传超过5张" });
      }
    },
    //拍照

    cameraTakePicture(mySourceType) {
      //把缓存里的takephoto里的丢进onsuces
      let that = this;
      navigator.camera.getPicture(onSuccess, onFail, {
        quality: 50,
        destinationType: Camera.DestinationType.DATA_URL,
        encodingType: Camera.EncodingType.JPEG,
        sourceType: mySourceType
      });

      function onSuccess(imageData) {
        var imageurl = "data:image/jpeg;base64," + imageData;
        var lng = localStorage.getItem("currentLat");
        var lat = localStorage.getItem("currentLng");
        that.showTime(); //获取当前时间
        var ret = {};
        ret.createtime = that.currentDateTime;
        ret.position = { lon: lng, lat: lat };
        //查看大小
        var str = imageData;
        var equalIndex = str.indexOf("=");
        if (str.indexOf("=") > 0) {
          str = str.substring(0, equalIndex);
        }
        var strLen = str.length;
        var fileSize = strLen - (strLen / 8) * 2;
        var size = (fileSize / 1024).toFixed(2);
        if (parseInt(size) > 1024) {
          compressImage(imageurl, {
            width: 200,
            quality: 0.4 // 压缩后图片的宽
            // 压缩后图片的高，宽高若只传一个，则按图片原比例进行压缩
            // 压缩后图片的清晰度，取值0-1，不传默认为0.7，值越小，所绘制出的图像越模糊
          }).then(result => {
            ret.url = result;
            that.photo.push(ret);
            that.TemplateKpi[that.countindex].photo = that.photo;
            if (that.TemplateKpi[that.countindex].photo.length > 0) {
              that.TemplateKpi[that.countindex].viewimg = that.viewImg1;
            }
          });
        } else {
          ret.url = imageurl;
          that.photo.push(ret);
          that.TemplateKpi[that.countindex].photo = that.photo;
          if (that.TemplateKpi[that.countindex].photo.length > 0) {
            that.TemplateKpi[that.countindex].viewimg = that.viewImg1;
          }
        }
      }

      function onFail(message) {
        this.$toast({ message: message });
      }
    },
    //拍照2
    twocameraTakePicture(mySourceType) {
      //把缓存里的takephoto里的丢进onsuces
      let that = this;
      navigator.camera.getPicture(onSuccess, onFail, {
        quality: 50,
        destinationType: Camera.DestinationType.DATA_URL,
        encodingType: Camera.EncodingType.JPEG,
        sourceType: mySourceType
      });

      function onSuccess(imageData) {
        var imageurl = "data:image/jpeg;base64," + imageData;
        var lng = localStorage.getItem("currentLat");
        var lat = localStorage.getItem("currentLng");
        that.showTime(); //获取当前时间
        var ret = {};
        ret.createtime = that.currentDateTime;
        ret.position = { lon: lng, lat: lat };
        //查看大小
        var str = imageData;
        var equalIndex = str.indexOf("=");
        if (str.indexOf("=") > 0) {
          str = str.substring(0, equalIndex);
        }
        var strLen = str.length;
        var fileSize = strLen - (strLen / 8) * 2;
        var size = (fileSize / 1024).toFixed(2);
        if (parseInt(size) > 1024) {
          compressImage(imageurl, {
            width: 200,
            quality: 0.4 // 压缩后图片的宽
            // 压缩后图片的高，宽高若只传一个，则按图片原比例进行压缩
            // 压缩后图片的清晰度，取值0-1，不传默认为0.7，值越小，所绘制出的图像越模糊
          }).then(result => {
            ret.url = result;
            that.twophoto.push(ret);
            that.twoTemplateKpi[that.twocountindex].photo = that.twophoto;
            if (that.twoTemplateKpi[that.twocountindex].photo.length > 0) {
              that.twoTemplateKpi[that.twocountindex].viewimg = that.viewImg1;
            }
          });
        } else {
          ret.url = imageurl;
          that.twophoto.push(ret);
          that.twoTemplateKpi[that.twocountindex].photo = that.twophoto;
          if (that.twoTemplateKpi[that.twocountindex].photo.length > 0) {
            that.twoTemplateKpi[that.twocountindex].viewimg = that.viewImg1;
          }
        }
      }

      function onFail(message) {
        this.$toast({ message: message });
      }
    },
    //显示图片弹窗
    ViewImg(index) {
      var array = this.TemplateKpi[index].photo;
      if (array === undefined || array.length == 0) {
        this.$toast({ message: "未上传照片" });
      } else {
        this.photoimg = this.TemplateKpi[index].photo;
        console.log("弹出数组", this.photoimg);
        this.preViewshow = true;
        this.current = 0;
      }
    },
    //显示图片弹窗2
    twoViewImg(index) {
      var array = this.twoTemplateKpi[index].photo;
      console.log(1, array);
      if (array === undefined || array.length == 0) {
        this.$toast({ message: "未上传照片" });
      } else {
        this.twophotoimg = this.twoTemplateKpi[index].photo;
        console.log("弹出数组", this.twophotoimg);
        this.twopreViewshow = true;
        this.twocurrent = 0;
      }
    },
    //获取当前经纬度
    getLoation() {
      let _this = this;
      var geolocation = new BMap.Geolocation();
      // 开启SDK辅助定位
      geolocation.enableSDKLocation();
      geolocation.getCurrentPosition(function(r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
          const myGeo = new BMap.Geocoder();
          myGeo.getLocation(new BMap.Point(r.point.lng, r.point.lat), data => {
            if (data.addressComponents) {
              var result = data.addressComponents;
              var pathlocation = {
                creditLongitude: r.point.lng, // 经度
                creditLatitude: r.point.lat, // 纬度
                creditProvince: result.province || "", // 省
                creditCity: result.city || "", // 市
                creditArea: result.district || "", // 区
                creditStreet:
                  (result.street || "") + (result.streetNumber || "") // 街道
              };
              _this.currentLng = pathlocation.creditLongitude;
              _this.currentLat = pathlocation.creditLatitude;
              localStorage.setItem("currentLng", _this.currentLng);
              localStorage.setItem("currentLat", _this.currentLat);
            }
          });
        }
      });
    },
    //获取当前时间
    showTime() {
      var d = new Date();
      this.currentDateTime =
        d.getFullYear() +
        "-" +
        (d.getMonth() + 1) +
        "-" +
        d.getDate() +
        " " +
        d.getHours() +
        ":" +
        d.getMinutes() +
        ":" +
        d.getSeconds();
    },

    /*项目部*/
    //显示项目管理部列表
    initFirst() {
      var params = { token: this.token, group_department_id: 1 };
      this.$http
        .get(
          `${this.$basePath}?service=App.Organizational.ListGroupDepartment&api=84`,
          {
            params: params
          }
        )
        .then(res => {
          if (res.ret == 200) {
            console.log("项目部列表", res);
            var result = res.data.map(o => {
              return {
                text: o.name,
                value: o.id
              };
            });

            this.GroupDepartmentcolumns = result;
            var params = {
              group_department_id: 1,
              token: this.token
            };
            this.initSencond(params);
          } else {
            this.$toast({ message: res.msg });
          }
        });
    },

    /**项目公司 */
    initSencond(params) {
      this.$http
        .get(
          `${this.$basePath}?service=App.Organizational.ListProjectCompany&api=84`,
          {
            params: params
          }
        )
        .then(res => {
          if (res.ret == 200) {
            console.log("项目公司列表", res);
            var result = res.data.map(o => {
              return {
                text: o.name,
                value: o.id
              };
            });
            result.unshift({ text: "请选择项目公司", value: 0 });
            this.ProjectCompanycolumns = result;
          } else {
            this.$toast({ message: res.msg });
          }
        });
    },
    onConfirmProjectCompany() {
      this.clearnName = "";
      this.project_address = "";
      this.project_addrcode = "";
      this.two_project_address = "";
      this.two_project_addrcode = "";
      this.xzname = "请选择行政村1";
      this.twoxzname = "请选择行政村2";
      this.xzList = [];
      this.Project = 0;
      this.District = 0;
      this.List = [];

      if (this.ProjectCompany != 0) {
                var params1 = {
          project_company_id: this.ProjectCompany,
          token: this.token
        };
        this.getClearnName(params1);
        var params = {
          project_company_id: this.ProjectCompany,
          token: this.token
        };
        this.initThird(params);
      }
    },
    /**项目部 */
    initThird(params) {
      this.$http
        .get(
          `${this.$basePath}?service=App.Organizational.ListProject&api=84`,
          {
            params: params
          }
        )
        .then(res => {
          if (res.ret == 200) {
            console.log("项目部列表", res);
            var result = res.data.map(o => {
              return {
                text: o.name,
                value: o.id
              };
            });
            result.unshift({ text: "请选择项目部", value: 0 });
            this.Projectcolumns = result;
          } else {
            this.$toast({ message: res.msg });
          }
        });
    },
    onConfirmProject() {
      this.clearnName = "";
      this.project_address = "";
      this.project_addrcode = "";
      this.two_project_address = "";
      this.two_project_addrcode = "";
      this.xzname = "请选择行政村1";
      this.twoxzname = "请选择行政村2";
      this.xzList = [];
      if (this.Project != 0) {
        var params = {
          project_id: this.Project,
          token: this.token
        };
        this.initFour(params);
      }
    },
    /**行政村*/
    initFour(params) {
      this.$http
        .get(`${this.$basePath}?service=App.Project.GetVillage&api=86`, {
          params: params
        })
        .then(res => {
          if (res.ret == 200) {
            this.project_List = res.data;
            console.log("行政村列表", res);
            var result = res.data.map(o => {
              return {
                text: o.address,
                value: o.id
              };
            });
            this.xzList = result;
            // result.unshift({ text: "请选择行政村", value: 0 });
            this.Districtcolumns = result;
          } else {
            this.$toast({ message: res.msg });
          }
        });
    },
    onConfirmDistrict() {
      console.log("行政村数组", this.Districtcolumns);
      console.log("行政村索引", this.District);
      //匹配对象
      if (this.project_List.length !== 0) {
        var Districtcolumns = this.project_List;
        var District = this.District;
        var obj = Districtcolumns.find(function(obj) {
          return obj.id == District;
        });
        console.log("匹配对象", obj);
        this.project_address = obj.address;
        this.project_addrcode = obj.addrcode;
        console.log(this.project_address, this.project_addrcode);
        //  project_address: "", //行政村地址
        // project_addrcode:"",//编码
      }
    },
    chosexz(id, index) {
      this.tabCurrentIndex = index;
      console.log("行政村数组", this.Districtcolumns);
      console.log("行政村索引", this.District);
      //匹配对象
      if (this.project_List.length !== 0) {
        var Districtcolumns = this.project_List;
        var District = id;
        var obj = Districtcolumns.find(function(obj) {
          return obj.id == District;
        });
        console.log("匹配对象", obj);
        this.project_address = obj.address;
        this.project_addrcode = obj.addrcode;
        console.log(this.project_address, this.project_addrcode);
        this.xzname = this.project_address;
        this.xzshow = false;
      }
    },
    twochosexz(id, index) {
      this.twotabCurrentIndex = index;
      console.log("行政村数组", this.Districtcolumns);
      console.log("行政村索引", this.District);
      //匹配对象
      if (this.project_List.length !== 0) {
        var Districtcolumns = this.project_List;
        var District = id;
        var obj = Districtcolumns.find(function(obj) {
          return obj.id == District;
        });
        console.log("匹配对象", obj);
        this.two_project_address = obj.address;
        this.two_project_addrcode = obj.addrcode;
        console.log(this.two_project_address, this.two_project_addrcode);
        this.twoxzname = this.two_project_address;
        this.twoxzshow = false;
      }
    },
    /**获取项目公司信息 */
    getClearnName(params) {
      this.$http
        .get(
          `${this.$basePath}?service=App.ProjectCompany.GetProjectCompany&api=86`,
          {
            params: params
          }
        )
        .then(res => {
          this.is_power = res.data.is_power;
          console.log("项目公司信息", this.is_power);
          if (res.ret == 200 && res.data.name) {
            this.clearnName = res.data.name;
            console.log("项目公司信息", res);
          } else {
            this.clearnName = "";
            this.$toast({ message: "未分配项目公司" });
          }
        });
    },
    //指标信息
    getloadData() {
      this.$toast.loading({
        mask: true,
        message: "加载中...",
        duration: 5000
      });
      var params = { token: this.token, template_id: 2 };
      this.$http
        .get(`${this.$basePath}?service=App.TemplateKpi.GetKpiList&api=86`, {
          params: params
        })
        .then(res => {
          if (res.ret == 200) {
            this.$toast.clear();
            console.log("指标信息", res);
            this.TemplateKpiTop = res.data;
            //this.TemplateKpi = res.data;
            var reformattedArray = [];
            res.data.forEach(function(item) {
              reformattedArray.push({
                id: item.id,
                score: item.score,
                name: item.name
              });
            });
            console.log("提qu出来的数组1", reformattedArray);
            var that = this;
            this.TemplateKpi = reformattedArray.map(function(obj, index) {
              obj.newscore = obj.score; //添加id属性
              obj.viewimg = that.viewImghui;
              return obj; //如果不返回则输出： Array [undefined, undefined, undefined
            });
            console.log("新数组1", this.TemplateKpi);

            var reformattedArray1 = [];
            res.data.forEach(function(item) {
              reformattedArray1.push({
                id: item.id,
                twoscore: item.score,
                twoname: item.name
              });
            });
            console.log("提前出来的数组2", reformattedArray);

            this.twoTemplateKpi = reformattedArray1.map(function(obj, index) {
              obj.twonewscore = obj.twoscore; //添加id属性
              obj.viewimg = that.viewImghui;
              return obj; //如果不返回则输出： Array [undefined, undefined, undefined
            });

            console.log("新数组2", this.twoTemplateKpi);

            this.template_id = res.data[0].template_id;
          } else {
            this.$toast({ message: res.msg });
          }
        });
    },
    /*提交分数事件*/
    postdata(params) {
      this.$http
        .post(
          `${this.$basePath}?service=App.ProjectCompany.UpdateCleanCheck&api=86`,
          params
        )
        .then(res => {
          if (res.ret == 200) {
            this.$toast({ message: res.msg });
            this.showloading = false;
            console.log("提交分数", res);
            //提交后清空数据
            this.$router.push({
              //核心语句
              path: "/projectCompanyList" //跳转的路径
            });
          } else {
            this.$toast({ message: res.msg });
            this.showloading = false;
          }
        });
    },
    //数据保存中
    savedata() {
      var timestamp = Date.parse(new Date());
      if (
        this.project_addrcode.length == 0 ||
        this.two_project_addrcode.length == 0
      ) {
        this.$toast({ message: "请选择行政村" });
        return false;
      }
      //提交第一个行政村
      var result = [];
      console.log("添加对象", this.TemplateKpi);
      this.TemplateKpi.forEach(function(item) {
        result.push({
          id: item.id,
          score: item.score,
          photo: item.photo
        });
      });
      //提交第er个行政村
      var tworesult = [];
      this.twoTemplateKpi.forEach(function(item) {
        tworesult.push({
          id: item.id,
          score: item.twoscore,
          photo: item.photo
        });
      });
      //判断有么有空值
      let result1 = result.find(obj => {
        return obj.score == "";
      });
      let tworesult1 = tworesult.find(obj => {
        return obj.score == "";
      });
      console.log("是否有空值", result1);
      if (result1 != undefined || tworesult1 != undefined) {
        this.$toast({ message: "请填写完整分数", duration: 2000 });
      } else if (this.sumconunt > 100 || this.twosumconunt > 100) {
        this.$toast({ message: "总分超过100分，请重新打分" });
      } else {
        this.loaddingText = "数据保存中...";
        this.showloading = true;
        console.log("kpi", result);
        console.log("kpi2", tworesult);
        var params = {
          token: this.token,
          id: 0,
          group_department_id: this.user_group_department_id,
          project_company_id: this.ProjectCompany,
          project_id: this.Project,
          addrcode1: this.project_addrcode,
          addrcode2: this.two_project_addrcode,
          address1: this.project_address,
          address2: this.two_project_address,
          result1: JSON.stringify(result),
          result2: JSON.stringify(tworesult),
          statu: 0
        };
        console.log("params", params);
        this.postdata(params); //提交第一个行政村
      }
    },
    //数据提交
    submit() {
      var timestamp = Date.parse(new Date());
      if (
        this.project_addrcode.length == 0 ||
        this.two_project_addrcode.length == 0
      ) {
        this.$toast({ message: "请选择行政村" });
        return false;
      }
      //提交第一个行政村
      var result = [];
      console.log("添加对象", this.TemplateKpi);
      this.TemplateKpi.forEach(function(item) {
        result.push({
          id: item.id,
          score: item.score,
          photo: item.photo
        });
      });
      //提交第er个行政村
      var tworesult = [];
      this.twoTemplateKpi.forEach(function(item) {
        tworesult.push({
          id: item.id,
          score: item.twoscore,
          photo: item.photo
        });
      });
      //判断有么有空值
      let result1 = result.find(obj => {
        return obj.score == "";
      });
      let tworesult1 = tworesult.find(obj => {
        return obj.score == "";
      });
      console.log("是否有空值", result1);
      if (result1 != undefined || tworesult1 != undefined) {
        this.$toast({ message: "请填写完整分数", duration: 2000 });
      } else if (this.sumconunt > 100 || this.twosumconunt > 100) {
        this.$toast({ message: "总分超过100分，请重新打分" });
      } else {
        this.loaddingText = "数据保存中...";
        this.showloading = true;
        console.log("kpi", result);
        console.log("kpi2", tworesult);

        var params = {
          token: this.token,
          id: 0,
          group_department_id: this.user_group_department_id,
          project_company_id: this.ProjectCompany,
          project_id: this.Project,
          addrcode1: this.project_addrcode,
          addrcode2: this.two_project_addrcode,
          address1: this.project_address,
          address2: this.two_project_address,
          result1: JSON.stringify(result),
          result2: JSON.stringify(tworesult),
          statu: 1
        };
        console.log("params", params);
        this.postdata(params); //提交第一个行政村
      }
    },
    //系统日期
    showDate() {
      var d = new Date();
      let year = d.getFullYear();
      let month = d.getMonth() + 1;
      let day = d.getDate();
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      this.signdate = year + "-" + month + "-" + day;
    }
  }
};
</script>
<style lang="less" scoped>
@import "../../../assets/css/check";
.chosexz {
  height: 35px;
  margin: 0 0 10px;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 0 16px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #323233;
  line-height: 35px;
}
.popdiv {
  width: 100%;
}
.popitle {
  width: 100%;
  border-bottom: 1px solid #eee;
  padding: 15px 0;
  box-sizing: border-box;
  .searchxp {
    border: 1px solid #eee;
    border-radius: 40px;
    width: 80%;
    margin: 0 auto;
    padding: 6px;
    box-sizing: border-box;
  }
  .searchx {
    border: none;
    width: 100%;
    height: 20px;
    text-align: center;
  }
}
.pop_ul {
  width: 100%;
  height: calc(100% - 67px);
  overflow: scroll;
  .pop_li {
    width: 100%;
    height: 44px;
    border-bottom: 1px solid #eee;
    line-height: 44px;
    padding: 0 16px;
    box-sizing: border-box;
  }
}
.current {
  color: #0ad691;
}
.choseContent {
  .title {
    font-size: 14px;
    color: #fd6347;
    padding: 0 15px;
    box-sizing: border-box;
    margin-bottom: 0;
  }
}
</style>